package com.hui.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hui.dto.*;
import com.hui.entity.Room;
import com.hui.excel.RoomExcel;
import com.hui.exception.IllegalParamException;
import com.hui.exception.ServerErrorException;
import com.hui.exception.VersionException;
import com.hui.mapper.RoomMapper;
import com.hui.service.RoomService;
import com.hui.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "room")
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(RoomInsertDTO dto) {
        String info = dto.getInfo();
        // 拷贝属性
        Room room = BeanUtil.copyProperties(dto, Room.class);
        // 设置默认值
        room.setInfo(ObjectUtil.isNull(info) ? "暂无描述" : info);
        room.setVersion(0L);
        room.setDeleted(0);
        room.setCreated(LocalDateTime.now());
        room.setUpdated(LocalDateTime.now());
        // DB添加
        int result = roomMapper.insert(room);
        if (result <= 0) {
            throw new ServerErrorException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#p0",condition = "#p0 !=null", unless = "#result == null")
    @Override
    public Room select(Long id) {
        Room result = roomMapper.select(id);
        if (ObjectUtil.isNull(result)){
            throw new ServerErrorException("房间不存在");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null")
    @Override
    public List<RoomVO> list() {
        return roomMapper.list(new RoomPageDTO())
                .stream()
                .map(room -> BeanUtil.copyProperties(room, RoomVO.class))
                .collect(Collectors.toList());
    }

    @Cacheable(key = "#root.methodName+ ':' + #p0.toString() + ':' + #p0.getPageSize()", condition = "#p0 != null")
    @Override
    public PageInfo<Room> page(RoomPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(roomMapper.list(dto));

    }

    @CacheEvict(allEntries = true)
    @Override
    @Retryable(value = VersionException.class)
    public int update(RoomUpdateDTO dto) {
        Room room = roomMapper.select(dto.getId());
        if (ObjectUtil.isNull(room)){
            throw new ServerErrorException("记录不存在");
        }
        BeanUtil.copyProperties(dto, room);
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.update(room);
        if (result <= 0){
            throw new VersionException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Long id) {
        int result = roomMapper.delete(id);
        if (result <= 0){
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteBatch(List<Long> ids) {
        int result = roomMapper.deleteBatch(ids);
        if (result <= 0){
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }
    @Override
    public List<RoomExcel> getExcelData() {
        return roomMapper.list(new RoomPageDTO())
                .stream()
                .map(room -> BeanUtil.copyProperties(room, RoomExcel.class))
                .collect(Collectors.toList());
    }


}
